﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Media3D;
using Windows.Graphics.Display;
using Windows.Graphics.Imaging;
using Windows.UI.Xaml.Media.Animation;

// Die Elementvorlage "Leere Seite" ist unter http://go.microsoft.com/fwlink/?LinkId=234238 dokumentiert.

namespace UniversalAppTest
{
    /// <summary>
    /// Eine leere Seite, die eigenständig verwendet werden kann oder auf die innerhalb eines Frames navigiert werden kann.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void CreateBasicLighting()
        {
            ModelVisual3D lightModelVisual3D = new ModelVisual3D();
            viewport3D.Children.Add(lightModelVisual3D);

            Model3DGroup lightSources = new Model3DGroup();
            lightModelVisual3D.Content = lightSources;

            lightSources.Children.Add(new DirectionalLight(Colors.White, new Vector3D(1, 0.2, -0.3)));
            lightSources.Children.Add(new AmbientLight(Colors.LightGray));
        }

        private void CreateBasicMesh()
        {
            ModelVisual3D basicMeshModel = new ModelVisual3D();
            GeometryModel3D basicGeometryModel = new GeometryModel3D();
            MeshGeometry3D basicMeshGeometry = new MeshGeometry3D();
            viewport3D.Children.Add(basicMeshModel);
            basicMeshModel.Content = basicGeometryModel;
            basicGeometryModel.Geometry = basicMeshGeometry;

            basicGeometryModel.Material = new DiffuseMaterial(GameShapeColors.Shape_T);
            //basicGeometryModel.Material = new DiffuseMaterial(
            //    new ImageBrush(
            //            new BitmapImage(new Uri("../../assets/img/BeaverMeadow.jpg", UriKind.Relative))
            //        )
            //    );
            basicGeometryModel.BackMaterial = new DiffuseMaterial(Brushes.Blue);

            basicMeshGeometry.Positions = GameMeshGrid.MeshGrid;
            basicMeshGeometry.Normals = GameMeshNormals.MeshNormals;

            #region additional_stuff
            {
                Point3DCollection modelPositions = new Point3DCollection();

                modelPositions.Add(new Point3D(0, 0, 0));
                modelPositions.Add(new Point3D(1, 0, 0));
                modelPositions.Add(new Point3D(1, 1, 0));
                modelPositions.Add(new Point3D(0, 1, 0));

                modelPositions.Add(new Point3D(0, 0, -1));
                modelPositions.Add(new Point3D(1, 0, -1));
                modelPositions.Add(new Point3D(1, 1, -1));
                modelPositions.Add(new Point3D(0, 1, -1));


                //basicMeshGeometry.Positions = modelPositions;


                Int32Collection modelTriangleIndices = new Int32Collection();

                // Front
                modelTriangleIndices.Add(0);
                modelTriangleIndices.Add(1);
                modelTriangleIndices.Add(2);

                modelTriangleIndices.Add(0);
                modelTriangleIndices.Add(2);
                modelTriangleIndices.Add(3);

                // Top
                modelTriangleIndices.Add(3);
                modelTriangleIndices.Add(2);
                modelTriangleIndices.Add(6);

                modelTriangleIndices.Add(3);
                modelTriangleIndices.Add(6);
                modelTriangleIndices.Add(7);

                // Back
                modelTriangleIndices.Add(7);
                modelTriangleIndices.Add(6);
                modelTriangleIndices.Add(5);

                modelTriangleIndices.Add(7);
                modelTriangleIndices.Add(5);
                modelTriangleIndices.Add(4);

                // Bottom
                modelTriangleIndices.Add(1);
                modelTriangleIndices.Add(0);
                modelTriangleIndices.Add(4);

                modelTriangleIndices.Add(1);
                modelTriangleIndices.Add(4);
                modelTriangleIndices.Add(5);

                // Right
                modelTriangleIndices.Add(1);
                modelTriangleIndices.Add(5);
                modelTriangleIndices.Add(6);

                modelTriangleIndices.Add(1);
                modelTriangleIndices.Add(6);
                modelTriangleIndices.Add(2);

                // Left
                modelTriangleIndices.Add(3);
                modelTriangleIndices.Add(7);
                modelTriangleIndices.Add(4);

                modelTriangleIndices.Add(4);
                modelTriangleIndices.Add(0);
                modelTriangleIndices.Add(3);

            }
            #endregion

            //basicMeshGeometry.TriangleIndices = modelTriangleIndices;
            basicMeshGeometry.TriangleIndices = GameShapes.Shape_T;

            RotateTransform3D meshRotation = new RotateTransform3D();
            meshRotation.CenterX = 0.5;
            meshRotation.CenterY = 0.5;
            meshRotation.CenterZ = -0.5;

            AxisAngleRotation3D meshAxisAngleRotation = new AxisAngleRotation3D(new Vector3D(1, 1, 1), 0);
            meshRotation.Rotation = meshAxisAngleRotation;

            Transform3DGroup meshTransform3DGroup = new Transform3DGroup();

            meshTransform3DGroup.Children.Add(meshRotation);

            basicMeshModel.Transform = meshTransform3DGroup;

            DispatcherTimer rotationAnimation = new DispatcherTimer();

            rotationAnimation.Interval = TimeSpan.FromMilliseconds(23);

            rotationAnimation.Tick += (s, a) =>
            {
                meshAxisAngleRotation.Angle++;
            };

            //rotationAnimation.Start();
        }
    }
}
